package com.example.popup;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import core.exception.BaseException;

import java.util.Timer;
import java.util.TimerTask;


public class Alert {

    public static void info(final Context context, String message, DialogInterface.OnDismissListener onDismissListener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("");
        builder.setMessage(message);
/*
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                dialog.cancel();
            }
        });
*/
        final AlertDialog dialog = builder.create();
        dialog.setOnDismissListener(onDismissListener);
        dialog.show();

        Timer timer = new Timer();
        timer.schedule(new CloseDialogTimer(dialog), 2000);
    }

    static class CloseDialogTimer extends TimerTask {

        private Dialog dialog;

        CloseDialogTimer(Dialog dialog) {
            this.dialog = dialog;
        }

        @Override
        public void run() {
            dialog.dismiss();
        }
    }

    public static void error(final Activity activity, BaseException e) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("Error!");
        builder.setMessage(e.getReason());
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int i) {
                dialog.cancel();
                activity.finish();
            }
        });
        builder.create().show();
    }
}
